/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import NetCommunicator.NetCommunicator;
import Signer.Signer;
import WASACommunicator.WASACommunicator;
import jWASA.Sensor;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.TooManyListenersException;

/**
 *
 * @author gianmariospacagna
 */
public class Controller {

    Signer signer;
    WASACommunicator wCommunicator;
    NetCommunicator netCommunicator;
    String port;
    InetAddress serverLocation;
    int serverPort;
    Set<Sensor> sensors;

    public Controller() {
        this.signer = new Signer();
        sensors = new HashSet<Sensor>();
        addSensors();
    }

    public void setPort(String port) {
        this.port = port;
    }

    private void setServerLocation(InetAddress serverLocation) {
        this.serverLocation = serverLocation;
    }

    public void setServerLocation(String serverLocation) throws UnknownHostException {
        InetAddress add = Inet4Address.getByName(serverLocation);
        setServerLocation(add);
    }

    public void setServerPort(String text) {
        setServerPort(Integer.parseInt(text));
    }

    private void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public RTMeasurements startMonitoring(Set<Sensor> sensors, int rate) throws TooManyListenersException {
        addSensors(sensors);
        RTMeasurements rtm = new RTMeasurements(port, sensors, rate,signer, serverLocation, serverPort);
        rtm.start();
        return rtm;
    }

    private void addSensors() {
        //sensors.add(new Sensor("EKG", "Electrocardiogram Sensor", 2, 1));
        sensors.add(new Sensor("LIGHT", "Light Sensor", 3, 1));
        sensors.add(new Sensor("TEMP", "Temperature Sensor", 4, 1));
    }
    
    
    private void addSensors(Collection<Sensor> sensors){
        this.sensors.addAll(sensors);
    }

    public Set<Sensor> getSensors() {
        return sensors;
    }
}
